<?php

class SearchController extends Controller
{
  public $layout='//layouts/main';

  public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('members', 'all'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionMembers()
	{
    $user = new Users;
    if($_POST['member']){
      $data = $_POST['member'];

      $member_id = 1;
      $criteria = new CDbCriteria;
      //$criteria->join = 'LEFT JOIN photos ON photos.user_id = t.id';
      $criteria->condition = 'name LIKE "%'.$data.'%" OR province LIKE "%'.$data.'%" OR city LIKE "%'.$data.'%"';
      //$criteria->together = true;
      //$criteria->group = 'GROUP BY t.id';

      $users = School::model()->findAll($criteria);

      //$users = Users::model()->findBySql('SELECT a.*,b.content as profile_picture FROM users a, photos b WHERE a.id = b.user_id AND b.profile_picture = 1 AND a.name = :name', array(':name' => $data));
    }

		$this->render('members', array(
      'users' => $users,
    ));
	}
  
  public function actionMemberPremium()
	{
    $user = new Users;
    if($_POST['member']){
      $data = $_POST['member'];

      $member_id = 1;
      $criteria = new CDbCriteria;
      //$criteria->join = 'LEFT JOIN photos ON photos.user_id = t.id';
      $criteria->condition = 'name LIKE "%'.$data.'%" OR province LIKE "%'.$data.'%" OR city LIKE "%'.$data.'%"';
      //$criteria->together = true;
      //$criteria->group = 'GROUP BY t.id';

      $users = School::model()->findAll($criteria);

      //$users = Users::model()->findBySql('SELECT a.*,b.content as profile_picture FROM users a, photos b WHERE a.id = b.user_id AND b.profile_picture = 1 AND a.name = :name', array(':name' => $data));
    }

		$this->render('member_premium', array(
      'users' => $users,
    ));
	}

  public function actionAll()
	{
    if($_POST['keyword']){
      $data = $_POST['keyword'];
      $news = News::model()->search($data);
      $articles = Articles::model()->search($data);
      $links = Links::model()->search($data);
    }

		$this->render('all', array(
      'news' => $news,
      'articles' => $articles,
      'links' => $links,
    ));
	}
}